package com.geeyuu.android.activity;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.geeyuu.android.R;
import com.geeyuu.android.model.BaseUserInfo;
import com.geeyuu.android.utils.AndroidUtil;

public class Activity2 extends BaseActivity {
    
    private ListView mFriendListView;
    private ArrayList<BaseUserInfo> mFriendArrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.TAG = "Activity2";
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2_layout);
        
        initTitleViews();
        initContentViews();
        
    }
    
    private void initTitleViews() {
        Button topLeftButton = (Button)findViewById(R.id.top_left_button);
        topLeftButton.setVisibility(View.VISIBLE);
        topLeftButton.setText("对话");
        topLeftButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.title_icon_tab2, 0, 0, 0);
        topLeftButton.setPadding(AndroidUtil.dip2px(this, 10), 0, 0, 0);
        ViewGroup.LayoutParams vl = topLeftButton.getLayoutParams();
        if(null == vl){
            vl = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.FILL_PARENT);
        }else{
            vl.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            vl.height = ViewGroup.LayoutParams.FILL_PARENT;
        }
        topLeftButton.setLayoutParams(vl);
        
    }
    
    private void initContentViews() {
        BaseUserInfo user;
        mFriendArrayList = new ArrayList<BaseUserInfo>();
        for(int i=0; i<10; i++){
            user = new BaseUserInfo();
            mFriendArrayList.add(user);
        }
        
        mFriendListView = (ListView)findViewById(R.id.chat_friend_list_view);
        BaseAdapter baseAdapter = new FriendListAdapter(this, R.layout.friend_list_item);
        mFriendListView.setAdapter(baseAdapter);
        mFriendListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Activity0.isFromActivity0 = false;
                startActivity(new Intent(Activity2.this, ChatActivity.class));
            }
        });
        mFriendListView.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                return true;
            }
        });
        mFriendListView.setSelection(0);
    }
    
    class FriendListAdapter extends BaseAdapter{
        
        private final WeakReference<Activity2> activity2;
        private int count;
        private final int itemLayoutResId;
        
        public FriendListAdapter(Activity activity, int layoutResId){
            activity2 = new WeakReference<Activity2>((Activity2)activity);
            itemLayoutResId = layoutResId;
            count = activity2.get().mFriendArrayList.size();
        }
        @Override
        public int getCount() {
            return count;
        }
        @Override
        public Object getItem(int position) {
            return null;
        }
        @Override
        public long getItemId(int position) {
            return 0;
        }
        @Override
        public void notifyDataSetChanged() {
            count = activity2.get().mFriendArrayList.size();
            super.notifyDataSetChanged();
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            if(null == view){
                view = activity2.get().getLayoutInflater().inflate(itemLayoutResId, parent, false);
            }
            
            
            
            
            
            
            return view;
        }
        
    }
    
}
